/////////////////////////////////////////////
// ObjectARX defined commands

#include "StdAfx.h"
#include "StdArx.h"
#include "GeAssign.h"


// This is command 'INSRTBLK'
void insrtBlk()
{
	char blkName[50];
	AcDbDatabase *pCurDb;
	AcDbBlockTable *pBlkTable;
	AcDbBlockTableRecord *pBlkTableRecord;
	AcDbBlockReference *pInsrtObj;
	AcDbObjectId blkId;

	AcGePoint3d insPt;

	int retCode;

	retCode = acedGetString(0, "\nEnter Block Name: ", blkName);
	if(retCode != RTNORM || blkName[0] == '\0')
	{
		acutPrintf("\nInvalid block name.");
		return;
	}

	pCurDb = acdbHostApplicationServices()->workingDatabase();

	// Check to see if the block table
	// has blkName

	pCurDb->getBlockTable(pBlkTable, AcDb::kForRead);
	if(!pBlkTable->has(blkName))
	{
		acutPrintf("\nBlock definition %s not found. ", blkName);
		pBlkTable->close();
		return;
	}

	// Get the AcDbObjectId of the block
	// definition.
	pBlkTable->getAt(blkName, blkId);

	pBlkTable->getAt(ACDB_MODEL_SPACE, pBlkTableRecord, AcDb::kForWrite);
	pBlkTable->close();
	
	acedInitGet(RSG_NONULL, NULL);
	acedGetPoint(NULL, "\nPick insertion point: ", asDblArray(insPt));

	pInsrtObj = new AcDbBlockReference(insPt, blkId);

	// Here is where you can set scale, rotation and other
	// properties to the block entity. If you want to
	// see the AcDbBlockReference class for more details.

	pBlkTableRecord->appendAcDbEntity(blkId, pInsrtObj);

	pBlkTableRecord->close();
	pInsrtObj->close();
}

